package com.example.home.pet.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.pet.adapter.BeforeDataAdapter;
import com.example.home.pet.bean.BeforeDataBean;
import com.example.home.pet.bean.MeInquiryBean;
import com.example.home.tool.GeneralClass;
import com.example.home.tool.OKHttpTool;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import static com.example.home.tool.StaticActi.ip;

public class MeInquiryDetail extends AppCompatActivity {

    private MeInquiryBean.RowsBean rowsBean;
    private ImageView avatar;
    private TextView name;
    private TextView goodAt;
    private TextView practiceNo;
    private TextView titleTextView;
    private TextView jsonDataTextView;
    private ImageView img;
    private RecyclerView rcvList;
    private EditText inputEditText;
    private Button btnQuestion;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_inquiry_detail);
        Intent intent = getIntent();
        String meInId = intent.getStringExtra("meInId");
        Gson gson = new Gson();
        rowsBean = gson.fromJson(meInId, MeInquiryBean.RowsBean.class);
        initView();
        getBeforeData();
        QuestionClosely();
    }

    private void QuestionClosely() {
        String content = inputEditText.getText().toString();
        inputEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    if (TextUtils.isEmpty(content)) {
                        btnQuestion.setVisibility(View.VISIBLE);
                    } else {
                        btnQuestion.setVisibility(View.INVISIBLE);
                    }
                }
            }
        });
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("inquiryId",rowsBean.getDoctor().getTypeId());
            jsonObject.put("content",content);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (content != null){

            btnQuestion.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    OKHttpTool.doNetRequest("prod-api/api/pet-hospital/inquiry-message",
                            OKHttpTool.POST,
                            GeneralClass.class,
                            new OKHttpTool.NetCallback() {
                                @Override
                                public void onSuccess(Object obj) {
                                    GeneralClass generalClass = (GeneralClass) obj;
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            if (generalClass.getCode() == 200){
                                                Toast.makeText(MeInquiryDetail.this,"追问成功",Toast.LENGTH_LONG).show();
                                                MeInquiryDetail.this.finish();
                                            }else if (generalClass.getCode() == 500){
                                                Toast.makeText(MeInquiryDetail.this,"追问失败",Toast.LENGTH_LONG).show();
                                            }
                                        }
                                    });
                                }

                                @Override
                                public void onFailure(String msg) {

                                }
                            },jsonObject.toString());
                }
            });
        }

    }

    private void getBeforeData() {
        OKHttpTool.doNetRequest("prod-api/api/pet-hospital/inquiry-message/list?pageNum=1&pageSize=10&inquiryId="+rowsBean.getDoctor().getTypeId(),
                OKHttpTool.GET,
                BeforeDataBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        BeforeDataBean beforeDataBean = (BeforeDataBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindContent(beforeDataBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindContent(BeforeDataBean beforeDataBean) {
        BeforeDataAdapter beforeDataAdapter = new BeforeDataAdapter(MeInquiryDetail.this,beforeDataBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MeInquiryDetail.this);
        rcvList.setAdapter(beforeDataAdapter);
        rcvList.setLayoutManager(linearLayoutManager);
    }

    private void initView() {
        titleTextView = findViewById(R.id.titleTextView);
        jsonDataTextView = findViewById(R.id.jsonDataTextView);
        img = findViewById(R.id.img);
        rcvList = findViewById(R.id.rcv_list);
        inputEditText = findViewById(R.id.inputEditText);
        btnQuestion = findViewById(R.id.btn_question);

        avatar = findViewById(R.id.avatar);
        name = findViewById(R.id.name);
        goodAt = findViewById(R.id.goodAt);
        practiceNo = findViewById(R.id.practiceNo);

        Glide.with(MeInquiryDetail.this).load(ip+rowsBean.getImageUrls()).into(img);
        jsonDataTextView.setText(rowsBean.getQuestion());
        Glide.with(MeInquiryDetail.this).load(ip+rowsBean.getDoctor().getAvatar()).into(avatar);
        name.setText(rowsBean.getDoctor().getName());
        goodAt.setText("职称："+rowsBean.getDoctor().getJobName());
        practiceNo.setText("执业编号："+rowsBean.getDoctor().getPracticeNo());

    }
}